package com.globalwave.base;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;
import org.apache.struts2.ServletActionContext;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.globalwave.common.Convertor;
import com.globalwave.common.cache.CodeHelper;
import com.globalwave.system.entity.SessionUser;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

    protected Log log = LogFactoryImpl.getLog(this.getClass()) ;
    
    protected int pageIndex = 1 ;
    protected int pageSize = 30 ;
    
    protected Long[] ids ;
    protected String paramsAsXml ;
    

    public String grid() throws Exception {
        
        return "jsp" ;    
        
    }
    

    public String form() throws Exception {
        
        return "jsp" ;    
        
    }
    // setter & getter
    public int getPageIndex() {
        return pageIndex;
    }
    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public Long[] getIds() {
		return ids;
	}
	public void setIds(Long[] ids) {
		this.ids = ids;
	}
	
	public String getParamsAsXml() {
		return paramsAsXml;
	}


	public void setParamsAsXml(String paramsAsXml) {
		this.paramsAsXml = paramsAsXml;
	}


	/**
     * 直接输出.
     * 
     * @param contentType 内容的类型.html,text,xml的值见后，json为"text/x-json;charset=UTF-8"
     */
    protected void render(String text,String contentType){
        HttpServletResponse response = ServletActionContext.getResponse() ;
        try {
            response.setContentType(contentType);
            //response.getWriter().write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") ;
            response.getWriter().write(text);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 直接输出纯字符串.
     */
    protected void renderText(String text) {
        render(text,"text/plain;charset=UTF-8");
    }

    /**
     * 直接输出纯HTML.
     */
    protected void renderHtml(String text) {
        render(text,"text/html;charset=UTF-8");
    }

    /**
     * 直接输出纯XML.
     */
    public void renderXML(String text) {
        render(text,"text/xml;charset=UTF-8");
    }

    protected Element getSuccessDeleteMessage() throws Exception {
        return getMessage(null,"已经成功删除数据！") ;    
    }

    protected Element getSuccessUpdateMessage() throws Exception {
        return getMessage(null,"已经成功更新数据！") ;    
    }
    
    protected Element getSuccessCreateMessage(Object object) throws Exception {

        return this.getMessage(object, "已经成功创建数据!") ;
             
    }

    protected Element getSuccessUpdateMessage(Object object) throws Exception {
        return getMessage(object,"已经成功更新数据！") ;    
    }
    
    public Element getMessage(Object object,String msg) throws Exception {

        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("root") ;
        
        root.addElement("message").addText(msg) ;
        
        if (object != null) {
            root.addElement("data").add(Convertor.object2Xml(object)) ;
        }
        
        root.addElement("versionId").addText(String.valueOf(CodeHelper.getVersionId())) ;
        
        return root ;
             
    }
    
    public HttpServletRequest getRequest() {
        ActionContext ac = ActionContext.getContext() ;

        return (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);      
    }
    
    public HttpServletResponse getResponse() {
        ActionContext ac = ActionContext.getContext() ;

        return (HttpServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE);       
    }
    
    public HttpSession getSession() {
        return getRequest().getSession();       
    }
    
    protected ServletContext getServletContext() {
        ActionContext ac = ActionContext.getContext() ;

        return (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);    
    }
    
    protected WebApplicationContext getWebApplicationContext() {
        ActionContext ac = ActionContext.getContext() ;

        return WebApplicationContextUtils.getWebApplicationContext(
                (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT));        
    }
    
    protected SessionUser getSessionUser() {

        final SessionUser user = 
            (SessionUser) this.getSession().getAttribute(SessionUser.SESSION_PK) ;
        
        return user ;
    }
}
